package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分类管理分页查询首页
    @GetMapping("/page")
    public Result<PageResult> page( CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类管理分页查询首页:{}", categoryPageQueryDTO);
        System.out.println("222");
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    //修改分类接口
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类:{}", categoryDTO);
        categoryService.updata(categoryDTO);
        return Result.success();
    }
    //修改分类状态接口
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("修改分类状态:{}",status);
        categoryService.updataStatus(status,id);
        return Result.success();
    }

    @PostMapping //新增分类
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    //删除分类
    @DeleteMapping
    public Result delete(Long id){
        log.info("删除分类:{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    //根据类型查询分类
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        log.info("根据类型查询分类:{}",type);
        //查询分类
        List<Category> category = categoryService.list(type);
        System.out.println("菜品管理查询使用的同一个接口");

        return Result.success(category);
    }







}
